<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset = "UTF-8">
    <script src="{{url_for('static',filename='js/jquery.min.js')}}"></script>
    <script src="{{url_for('static',filename='js/bootstrap.min.js')}}"></script>
    <script src="{{url_for('static',filename='js/jquery.dataTables.min.js')}}"></script>
    <link rel="stylesheet" href="{{url_for('static',filename='css/jquery.dataTables.min.css')}}">
    <link rel="stylesheet" href="{{url_for('static',filename='css/bootstrap.min.css')}}">
    <title>等价概念</title>
        <style>
    .button{
        background-color: white;
        color: black;
        border: 2px solid #4CAF50;
    }

    .button:hover {
        background-color: #4CAF50;
        color: white;
    }

    .button[disabled] {
        opacity: 0.6;
        cursor: not-allowed;
    }

    </style>
</head>
<body>
<nav class="navbar navbar-default" role="navigation">
    <div class="container-fluid">
    <div class="navbar-header">
        <a class="navbar-brand" href="#">盯盘宝后台管理系统</a>
    </div>
    <div>
        <ul class="nav navbar-nav">
            <li ><a href="{{ url_for('start_web') }}">首页</a></li>
            <li ><a href="{{ url_for('permission_manage') }}">权限配置</a></li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                    管理员{{ session['admin'] if session['admin'] else "未登录" }}
                    <b class="caret"></b>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="{{ url_for("login_out") }}">退出</a></li>
                </ul>
            </li>
        </ul>
    </div>
    </div>
</nav>

<table colume="center" class="table table-striped table-bordered"  style="width:100%">
    <tr>
        <td>
            <div id="equivalent_concept_add" >

            <div style="margin:auto">
            等价概念:<input type="text" id="original_concept">
            主概念:<input type="text" id="normalization_concept">
            <button class="button" onclick="addEquivalentConcept('add')">添加</button>
            <button class="button" onclick="addEquivalentConcept('del')">删除</button>
            </div>
            </div>
        </td>
        <td>
            <div id="concepts_link_add" >

            <div style="margin:auto">
            子概念:<input type="text" id="child_concept">
            母概念:<input type="text" id="father_concept">
            <button class="button" onclick="addConceptLink('add')">添加</button>
            <button class="button" onclick="addConceptLink('del')">删除</button>
            </div>
            </div>
        </td>
        <td>
            <div id="black_list_concept_add" >
            <div style="margin:auto">
            屏蔽概念:<input type="text" id="black_list_concept">
            <button class="button" onclick="addBlackListConcept('add')">添加</button>
            <button class="button" onclick="addBlackListConcept('del')">删除</button>
            </div>
            </div>
        </td>
    </tr>
    <tr>
        <td>
            <div id="equivalent_concept" >

            <h3 align="center">概念等价表</h3>
            <div style="margin:auto">

            <table id="tb_equivalent_concept" border="1" class="display" style="width:30%">
            </table>
            </div>
            </div>
        </td>
        <td>
            <div id="concepts_link" >

            <h3 align="center">行业关联表</h3>
            <div style="margin:auto">
            <table id="tb_concepts_link" border="1" class="display" style="width:30%">
            </table>
            </div>
            </div>
        </td>
        <td>
            <div id="black_list_concept" >

            <h3 align="center">屏蔽表</h3>
            <div style="margin:auto">

            <table id="tb_black_list_concept" border="1" class="display" style="width:30%">
            </table>
            </div>
            </div>
        </td>
    </tr>
        <tr>
        <td>
            <div id="block_stocks_add" >
            <div style="margin:auto">
            股票:<input type="text" id="block_stocks_add_stock">
            概念:<input type="text" id="block_stocks_add_concept">
            <button class="button" onclick="addBlockStocksConcept('add')">添加</button>
            <button class="button" onclick="addBlockStocksConcept('del')">删除(仅限手动)</button>
            </div>
            </div>
        </td>
        <td>
            <div id="black_list_concept_add_for_stock" >
            <div style="margin:auto">
            股票:<input type="text" id="black_for_stock">
            屏蔽概念:<input type="text" id="black_for_concept">
            <button class="button" onclick="addBlackListConceptForStock('add')">添加</button>
            <button class="button" onclick="addBlackListConceptForStock('del')">删除</button>
            </div>
            </div>
        </td>
        <td>
            <p></p>
        </td>
    </tr>
        <tr>
        <td>
            <div id="block_stocks" >
                <button type="button" class="btn btn-primary" onclick="refreshBlockStocks()">重新生成</button>
            <h3 align="center">板块股票对应表</h3>
    <div class="row" align="center">
    <div class="btn-group" >
          <button id="source-button" type="button" class="btn btn-primary">全部</button>
          <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <span class="caret"></span>
            <span class="sr-only">Toggle Dropdown</span>
          </button>
          <ul class="dropdown-menu">
            <li><a href="#" onclick="selectSource('全部')">全部</a></li>
            <li><a href="#" onclick="selectSource('主概念')">主概念</a></li>
            <li><a href="#" onclick="selectSource('等价概念')">等价概念</a></li>
            <li><a href="#" onclick="selectSource('无主概念')">无主概念</a></li>
          </ul>
    </div>
    </div>

            <div style="margin:auto">

            <table id="tb_block_stocks" border="1" class="display" style="width:30%">
            </table>
            </div>
            </div>
        </td>
        <td>
            <div id="black_list_concept_for_stock" >

            <h3 align="center">个股屏蔽表</h3>
            <div style="margin:auto">

            <table id="tb_black_list_concept_for_stock" border="1" class="display" style="width:30%">
            </table>
            </div>
            </div>
        </td>
    </tr>
</table>
<script>
function refreshBlockStocks() {
    $.ajax({
        url:"/refreshBlockStocks",
        async:true,
        success:function (result) {
            if ($.type(result)=="string"){
                result = JSON.parse(result);
            }
            alert(result.message);
        }
    })
}

function selectSource(type){
     $("#source-button").text(type);
     if (type == "全部"){
         type = "";
     }
    block_stocks_table.column(3).search(type).draw();
}
$('#tb_equivalent_concept').DataTable({
{#"paging":   false,#}
"ordering": false,
"info":     false,
data: [],
columns: [
    { title: "等价概念" },
    { title: "主概念" }
]
});

$('#tb_concepts_link').DataTable({
{#"paging":   false,#}
"ordering": false,
"info":     false,
data: [],
columns: [
    { title: "子概念" },
    { title: "母概念" }
]
});

$('#tb_black_list_concept').DataTable({
{#"paging":   false,#}
"ordering": false,
"info":     false,
data: [],
columns: [
    { title: "屏蔽概念" }
]
});

$('#tb_black_list_concept_for_stock').DataTable({
{#"paging":   false,#}
"ordering": false,
"info":     false,
data: [],
columns: [
    {title:"股票"},
    { title: "屏蔽概念" }
]
});

var block_stocks_table = $('#tb_block_stocks').DataTable({
                            {#"paging":   false,#}
                            "ordering": false,
                            "info":     false,
                            data: [],
                            columns: [
                                { title: "股票" },
                                { title: "板块" },
                                {title:"来源"},
                                {title:"概念类型"}
                            ]
                            });

function reloadData(dataList,type) {
    var realTable=null;
    realTable = $('#'+type).DataTable();
    var currentPage = realTable.page();
    realTable.clear();
    realTable.rows.add(dataList);
    realTable.page(currentPage).draw( false );
}

function refreshData() {
     $.ajax({
         url:"/equivalentConceptData",
         async:true,
         success:function (result) {
             if (jQuery.type(result)=="string"){
                  var obj = JSON.parse(result);
             }
             else{
                 var obj = JSON.parse(result);
             }
             var tb_equivalent_concepts_list = obj.tb_equivalent_concepts_list;
             var tb_concepts_link_list = obj.tb_concepts_link_list;
             var tb_black_list_concept_list = obj.tb_black_list_concept_list;
             var tb_block_stocks_list = obj.tb_block_stocks_list;
             var tb_black_list_concept_for_stock_list = obj.tb_black_list_concept_for_stock_list;
             reloadData(tb_equivalent_concepts_list,"tb_equivalent_concept");
             reloadData(tb_concepts_link_list,"tb_concepts_link");
             reloadData(tb_black_list_concept_list,"tb_black_list_concept");
             reloadData(tb_block_stocks_list,"tb_block_stocks");
            reloadData(tb_black_list_concept_for_stock_list,"tb_black_list_concept_for_stock");
         }
     });
 }
refreshData();
function addEquivalentConcept(type){
    var originalConcept = $("#original_concept").val();
    var normalizationConcept = $("#normalization_concept").val();
    if (originalConcept == '' | normalizationConcept == ''){
        window.alert("等价概念或主概念为空，请填写后添加");
        return;
    }
    if (type == 'add'){
        var alertStr = "您确定要添加 \n " +
                "等价概念:" + originalConcept + "\n" +
                "主概念:" + normalizationConcept + "\n 吗？";
    }
    if (type == 'del'){
        var alertStr = "您确定要删除 \n " +
                "等价概念:" + originalConcept + "\n" +
                "主概念:" + normalizationConcept + "\n 吗？";
    }

    var go = window.confirm(alertStr);
    if (go == false) {
        return;
    }
    $.ajax({
    url: "/updateEquivalentConcept",
    async: true,
    type: 'POST',
    data: JSON.stringify({'table': "tb_equivalent_concepts", "type":type,'original_concept': originalConcept,
        'normalization_concept':normalizationConcept}),
    dataType: 'json',
    success: function (result) {
        var message = result.message;
        alert(message);
    }
});
}

function addConceptLink(type){
    var child_concept = $("#child_concept").val();
    var father_concept = $("#father_concept").val();
    if (child_concept == '' | father_concept == ''){
        window.alert("子概念或母概念为空，请填写后添加");
        return;
    }
    if (child_concept == father_concept ){
        window.alert("子概念与母概念不能相等");
        return;
    }
    if (type=="add"){
        var alertStr = "您确定要添加 \n " +
                "子概念:" + child_concept + "\n" +
                "母概念:" + father_concept + "\n 吗？";
    }

    if (type=="del"){
        var alertStr = "您确定要删除 \n " +
                "子概念:" + child_concept + "\n" +
                "母概念:" + father_concept + "\n 吗？";
    }

    var go = window.confirm(alertStr);
    if (go == false) {
        return;
    }
    $.ajax({
    url: "/updateEquivalentConcept",
    async: true,
    type: 'POST',
    data: JSON.stringify({'table': "tb_concepts_link","type":type, 'child_concept': child_concept,
        'father_concept':father_concept}),
    dataType: 'json',
    success: function (result) {
        var message = result.message;
        alert(message);
    }
});
}

function addBlackListConcept(type){
    var black_list_concept = $("#black_list_concept").val();
    if (black_list_concept == ''){
        window.alert("屏蔽概念为空，请填写后添加");
        return;
    }
    if (type=="add"){
        var alertStr = "您确定要添加 \n " +
                "屏蔽概念:" + black_list_concept + "\n 吗？";
    }
    if (type=="del"){
        var alertStr = "您确定要删除 \n " +
                "屏蔽概念:" + black_list_concept + "\n 吗？";
    }

    var go = window.confirm(alertStr);
    if (go == false) {
        return;
    }
    $.ajax({
    url: "/updateEquivalentConcept",
    async: true,
    type: 'POST',
    data: JSON.stringify({'table': "tb_black_list_concept","type":type, 'black_list_concept': black_list_concept}),
    dataType: 'json',
    success: function (result) {
        var message = result.message;
        alert(message);
    }
});
}

function addBlockStocksConcept(type){
    var stock = $("#block_stocks_add_stock").val();
    var concept = $("#block_stocks_add_concept").val();
    if (stock == '' | concept == ''){
        window.alert("股票或概念为空，请填写后添加");
        return;
    }
    if (stock.indexOf("SH")!=0 & stock.indexOf("SZ")!=0){
        window.alert("股票代码以SH或SZ开头");
        return;
    }
    if (type == 'add'){
        var alertStr = "您确定要添加 \n " +
                "股票:" + stock + "\n" +
                "概念:" + concept + "\n 吗？";
    }
    if (type == 'del'){
        var alertStr = "您确定要删除 \n " +
                "股票:" + stock + "\n" +
                "概念:" + concept + "\n 吗？";
    }
    var go = window.confirm(alertStr);
    if (go == false) {
        return;
    }
    $.ajax({
    url: "/updateEquivalentConcept",
    async: true,
    type: 'POST',
    data: JSON.stringify({'table': "tb_linked_stock", "type":type,'stock': stock,
        'concept':concept}),
    dataType: 'json',
    success: function (result) {
        var message = result.message;
        alert(message);
    }
});
}

function addBlackListConceptForStock(type){
    var stock = $("#black_for_stock").val();
    var concept = $("#black_for_concept").val();
    if (stock == '' | concept == ''){
        window.alert("股票或概念为空，请填写后添加");
        return;
    }
    if (stock.indexOf("SH")!=0 & stock.indexOf("SZ")!=0){
        window.alert("股票代码以SH或SZ开头");
        return;
    }
    if (type == 'add'){
        var alertStr = "您确定要添加 \n " +
                "股票:" + stock + "\n" +
                "屏蔽概念:" + concept + "\n 吗？";
    }
    if (type == 'del'){
        var alertStr = "您确定要删除 \n " +
                "股票:" + stock + "\n" +
                "屏蔽概念:" + concept + "\n 吗？";
    }
    var go = window.confirm(alertStr);
    if (go == false) {
        return;
    }
    $.ajax({
    url: "/updateEquivalentConcept",
    async: true,
    type: 'POST',
    data: JSON.stringify({'table': "tb_black_list_concept_for_stock", "type":type,'stock': stock,
        'concept':concept}),
    dataType: 'json',
    success: function (result) {
        var message = result.message;
        alert(message);
    }
});
}
</script>

</body>
</html>